<?php
namespace app\viphry\controller;

use think\Controller;
use app\viphry\model\Member;
use app\viphry\model\AgentMember;
use think\Db;
use think\Request;

class Agent extends Common
{

    const PERPAGE = 10;
    public function _initialize(){
        parent::_initialize();
        if(!in_array(ACTION_NAME,['loginin'])){
            CheckAgentMemberLogin();
        }

    }

    /**
     * 代理商登录接口
     * @author hey_heyang(635870453@qq.com)
     */

    public function loginin(Request $request){
        if(request()->isPost()){
            $agModel = new AgentMember();
            $param = $request->param();
            if(empty($param['login_name']) || empty($param['password'])){
                ajaxReturn('请将用户名和密码填写完整','1',203,'');
            }
            $where['login_name'] = ['eq',trim($param['login_name'])];
            $user_info = $agModel->getUserInfo($where);
            if($user_info){
                $password = sha1(md5(trim($param['password']) . $user_info['salt']));
                if ($password == $user_info['password']){
                    $token = agnetAccessToken();
                    $data['login_time'] = time();
                    $data['token'] = $token;
                    $res=$agModel->where(['id'=>$user_info['id']])->update($data);
                    if(!$res){
                        ajaxReturn('登录出错,请重新登录!','1',203,'');
                    }
                    session('agent_member_id', $user_info['id']);
                    session('agent_member_token',$token);
                    session('agent_last_login_time',time());
                    ajaxReturn('登录成功','1',200,$data);
                } else {
                    ajaxReturn('密码错误','1',201,'');
                }

            }else{
                ajaxReturn('未查询到该账户信息','1',204,'');
            }
            }else{
                ajaxReturn('参数错误','1',207,'');
        }
        }


    /**
     * 代理商获取今日会员
     * @author hey_heyang(635870453@qq.com)
     */

    public function getTodayNewMember(){
        $agModel = new AgentMember();
        if(empty($param['curpage'])){
            $curpage = 1;
        }else{
            $curpage = $param['curpage'];
        }
        $agent_info = $agModel->where(['id'=>session('agent_member_id')])->find()->toArray();
        $data = $agModel->getTodayMemberList($agent_info['related_member_id']);
        $total_count = count($data['memberList']);
        $total_page = ceil($total_count/self::PERPAGE);
        $data_list = pageArray($data['memberList'],10,$curpage);
        $res_data['total_count'] = $total_count;
        $res_data['total_page'] = $total_page;
        $res_data['result'] = $data_list;
        $res_data['member_total'] = $data['memberTotal'];
        ajaxReturn('请求成功','1',200,$res_data);

    }


    /**
     * 查询代理商旗下指定会员
     * @author hey_heyang(635870453@qq.com)
     */
    public  function getTargetMember(Request $request){
        if(request()->isPost()){
            $param = $request->param();
            if(empty($param['curpage'])){
                $curpage = 1;
            }else{
                $curpage = $param['curpage'];
            }
            if(empty($param['serach'])){
                ajaxReturn('请输入搜索的手机号或者姓名','1',203,'');
            }
            $agModel = new AgentMember();
            $agent_info = $agModel->where(['id'=>session('agent_member_id')])->find()->toArray();
            $data = $agModel->getTargetList($agent_info['related_member_id'],trim($param['serach']));
            $total_count = count($data);
            $total_page = ceil($total_count/self::PERPAGE);
            $data_list = pageArray($data,10,$curpage);
            $res_data['total_count'] = $total_count;
            $res_data['total_page'] = $total_page;
            $res_data['result'] = $data_list;
            ajaxReturn('请求成功','1',200,$res_data);
        }


    }

    /**
     * 查询会员投资记录
     * @author hey_heyang(635870453@qq.com)
     */
    public function getMemberOrderList(){
        if(request()->isPost()){
            $param = request()->param();
            if(empty($param['curpage'])){
                $curpage = 1;
            }else{
                $curpage = $param['curpage'];
            }
            if(empty($param['member_id'])){
                ajaxReturn('参数错误','1',203,'');
            }
            $agModel = new AgentMember();
            $data = $agModel->getOrderList($param['member_id']);
            $total_count = count($data);
            $total_page = ceil($total_count/self::PERPAGE);
            $data_list = pageArray($data,10,$curpage);
            $res_data['total_count'] = $total_count;
            $res_data['total_page'] = $total_page;
            $res_data['result'] = $data_list;
            ajaxReturn('请求成功','1',200,$res_data);
        }

    }







}
